﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Xcap
{
    #region Iconfig Interface

    /// <summary>
    /// IConfig defines data access interface
    /// </summary>
    public interface IConfig
    {
        /// <summary>
        /// Gets or sets a server domain used by application
        /// </summary>
        string host { get; set; }

        /// <summary>
        /// Gets or sets a value of server port used by specified protocol
        /// </summary>
        int port { get; set; }

        /// <summary>
        /// Gets or sets an application name
        /// </summary>
        string userAgent { get; set; }
    }

    #endregion

    #region Configuration

    public class Config
    {
        private string host;
        private int port;
        private string userAgent;
        private string serverRoot = "xcap-root/";   //defaultne nastavim root

        public string Host
        {
            get { return host; }
            set { host = value; }
        }

        public int Port
        {
            get { return port; }
            set { port = value; }
        }

        public string UserAgent
        {
            get { return userAgent; }
            set { userAgent = value; }
        }

        public string ServerRoot
        {
            get { return serverRoot; }
            set { serverRoot = value; }
        }
    }

    #endregion

    #region Account

    public class Account
    { 
        private string sipUri;
        private string login;       //login je ta cast medzi sip: a @
        private string password;
        //private string displayName;

        public Account(string _sipUri, string _password)
        {
            sipUri = _sipUri;
            password = _password;
            login = ExtractLogin(_sipUri);
        }

        public string SipUri 
        { 
            get{ return sipUri; }
            //set{ sipUri = value; }
        }

        public string Login
        {
            get { return login; }
            //set { password = value; }
        }

        public string Password
        {
            get { return password; }
            //set { password = value; }
        }

        /// <summary>
        /// Vyparsuje cez regex login zo vzorky sip:login@domena.sk
        /// </summary>
        /// <param name="contents"></param>
        /// <returns>string login</returns>
        private string ExtractLogin(string contents)
        {
            Regex pattern;
            Match match;
            pattern = new Regex(@"sip:([\w\s,.:'-]+)@", RegexOptions.IgnoreCase);
            match = pattern.Match(contents);
            if (match.Success)
                return match.Groups[1].Value;
            else
                throw(new Exception("An error has occured while parsing login"));
        }

    }

    #endregion
}
